# coding: utf-8

import argparse
import hashlib

import requests

from check_de_embassy import compare_and_store, message_result


URL = 'https://www.visametric.com/Germany/ru/app/19/{passport}/{code}'

def check(passport, code):
    resp = requests.get(URL.format(passport=passport, code=code))
    digest = hashlib.sha256(resp.text).hexdigest()
    not_changed = compare_and_store(digest)
    return (resp.text, not_changed)


def main():
    parser = argparse.ArgumentParser(description='Check schengen visa status.')
    parser.add_argument('passport', metavar='N', help='passport')
    parser.add_argument('code', metavar='N', help='code')
    parser.add_argument('--from', help='from')
    parser.add_argument('--to', help='to, comma separated')

    args = parser.parse_args()
    status, not_changed = check(args.passport, args.code)
    result = status
    if not_changed:
        if not_changed is None:
            print('Old state is not known')
        else:
            print('The status has not changed')
    else:
        print(u'The status has changed: {}'.format(result))
        if args.to:
            message_result(result, vars(args)['from'], args.to)
